package com.bw.liuyushan.zhuangao4_weektest2.MVP.Activity;

import android.content.Intent;
import android.content.SharedPreferences;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.liuyushan.mvp_lib.View.BaseActivity;
import com.bw.liuyushan.zhuangao4_weektest2.Adapter.FragmentTableAdapter;
import com.bw.liuyushan.zhuangao4_weektest2.Adapter.FragmentUserAdapter;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment.Fragment_Table1;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment.Fragment_Table2;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment.Login_Fragment;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Fragment.Reg_Fragment;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Model.BaseModelTest;
import com.bw.liuyushan.zhuangao4_weektest2.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.zhuangao4_weektest2.R;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

/**
 * ZhuanGao4_WeekTest2
 * file name is : UserActivity
 * created by Ender on 2021/8/23 9:40.
 * author : 刘雨杉
 * Describe(作用):
 */
@Route(path = "/start/endFragment")
public class UserActivity extends BaseActivity<BasePresenterTest> implements IContrackTest.C_ViewTest {
    private com.google.android.material.tabs.TabLayout TabUser;
    private androidx.viewpager.widget.ViewPager vpUser;
    ArrayList<Fragment> userlist;
    ArrayList<String> struser;

    @Override
    public int bindLayout() {
        return R.layout.activity_user;
    }

    @Override
    public void initView() {
        //防止内存泄露
        ip=new BasePresenterTest(new BaseModelTest(),this);
        //EventBus注册
        EventBus.getDefault().register(this);

        TabUser = (TabLayout) findViewById(R.id.Tab_User);
        vpUser = (ViewPager) findViewById(R.id.vp_User);
    }

    @Override
    public void initDate() {
        SharedPreferences sp = getSharedPreferences("sp", MODE_PRIVATE);
        boolean isLogin = sp.getBoolean("isLogin", false);
        if (isLogin){

            ToastUtils.showShort("欢迎再次回到app");
            Intent intent = new Intent(UserActivity.this, MainActivity.class);

            startActivity(intent);
        }
        userlist = new ArrayList<>();
        userlist.add(new Reg_Fragment());
        userlist.add(new Login_Fragment());

        struser = new ArrayList<>();
        struser.add("注册");
        struser.add("登录");

        FragmentUserAdapter fragmentUserAdapter = new FragmentUserAdapter(getSupportFragmentManager(), userlist, struser);
        vpUser.setAdapter(fragmentUserAdapter);
        TabUser.setupWithViewPager(vpUser);

    }
    //接收粘性事件 注册成功自动切换到登录页面
    @Subscribe(sticky = true)
    public void EventBusget(String msg){
        if (msg.equals("注册成功")){
            vpUser.setCurrentItem(1);
        }
    }
    //接收粘性事件 点击自动切换到登录页面
    @Subscribe(sticky = true)
    public void EventBusget2(String msg){
        if (msg.equals("直接登录")){
            vpUser.setCurrentItem(1);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
